import 'package:ddstore/dd_notifier.dart';
import 'package:flutter/material.dart';
import 'package:ddstore/dd_store.dart';

class StorePage extends StatefulWidget {
  const StorePage({super.key});

  @override
  State<StorePage> createState() => _StorePageState();
}

class _StorePageState extends State<StorePage> with DDStoreMixin {
  @override
  void dispose() {
    debugPrint("页面释放");
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: DDBuilder<String>(
          valueKey: "test1",
          defaultValue: "Store 配置",
          builder: (context, value, child) {
            return Text(value ?? '');
          },
        ),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            Container(
              color: Colors.cyan.withAlpha(10),
              child: Column(
                children: [
                  const Text("主视图"),
                  TextButton(
                    onPressed: () {
                      $store.setValue<String>(key: "test1", value: "test1");
                    },
                    child: const Text("修改页面标题为test1"),
                  ),
                  TextButton(
                    onPressed: () {
                      $store.setValue<Color>(key: "test2", value: Colors.green.withAlpha(40));
                    },
                    child: const Text("修改子视图1，子视图2的背景色为绿色"),
                  ),
                  TextButton(
                    onPressed: () {
                      $store.emit("test1");
                    },
                    child: const Text("派发事件test1，子视图1和子视图2和主视图相应事件"),
                  ),
                  TextButton(
                      onPressed: () async {
                        await $store.emit("test2", type: DDActionFutureType.wait);
                        debugPrint("等待其他事件完成");
                      },
                      child: const Text("派发test2事件，等待主视图，子视图1，子视图2全部完成")),
                  TextButton(
                      onPressed: () async {
                        await $store.emit("test2", type: DDActionFutureType.any);
                        debugPrint("其中任意一件事完成");
                      },
                      child: const Text("派发test2事件，等待主视图，子视图1，子视图2任意一个完成")),
                ],
              ),
            ),
            const Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                StoreSubPage1(),
                StoreSubPage2(),
              ],
            ),
          ],
        ),
      ),
    );
  }

  @override
  bool get forceRootStore => true;

  @override
  List<DDAction> get storeActions => [
        DDAction(
            name: "test1",
            callback: () {
              debugPrint("主视图，收到test1事件");
            }),
        DDAction(
            name: "test2",
            callback: () async {
              debugPrint("主视图收到test2事件,然后延迟2s");
              await Future.delayed(const Duration(seconds: 2));
            }),
      ];
}

class StoreSubPage1 extends StatefulWidget {
  const StoreSubPage1({super.key});

  @override
  State<StoreSubPage1> createState() => _StoreSubPage1State();
}

class _StoreSubPage1State extends State<StoreSubPage1> with DDStoreMixin {
  @override
  void dispose() {
    debugPrint("子视图1，释放");
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return DDBuilder<Color>(
        valueKey: "test2",
        defaultValue: Colors.black.withAlpha(40),
        builder: (context, value, child) {
          return Container(
            color: value,
            child: Column(
              children: [
                const Text("子视图1"),
                TextButton(
                    onPressed: () {
                      $store.setValue(key: "test1", value: "test2");
                    },
                    child: const Text("子视图1，修改页面标题为test2")),
                TextButton(
                    onPressed: () {
                      $store.setValue(key: "test2", value: Colors.blue.withAlpha(40));
                    },
                    child: const Text("子视图1,修改子视图1和2的颜色为蓝色")),
              ],
            ),
          );
        });
  }

  @override
  List<DDAction> get storeActions => [
        DDAction(
            name: "test1",
            callback: () {
              debugPrint("子视图1，收到test1事件");
            }),
        DDAction(
            name: "test2",
            callback: () async {
              debugPrint("子视图1收到test2事件,然后延迟5s");
              await Future.delayed(const Duration(seconds: 5));
            }),
      ];
}

class StoreSubPage2 extends StatefulWidget {
  const StoreSubPage2({super.key});

  @override
  State<StoreSubPage2> createState() => _StoreSubPage2State();
}

class _StoreSubPage2State extends State<StoreSubPage2> with DDStoreMixin {
  @override
  void dispose() {
    debugPrint("子视图2释放");
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return DDBuilder<Color>(
        valueKey: "test2",
        defaultValue: Colors.amber.withAlpha(20),
        builder: (context, value, child) {
          return Container(
            color: value,
            child: Column(
              children: [
                const Text("子视图2"),
                TextButton(
                    onPressed: () {
                      $store.setValue(key: "test1", value: "test3");
                    },
                    child: const Text("子视图2，修改页面标题为test3")),
                TextButton(
                    onPressed: () {
                      $store.setValue(key: "test2", value: Colors.red.withAlpha(40));
                    },
                    child: const Text("子视图1,修改子视图1和2的颜色为红色")),
              ],
            ),
          );
        });
  }

  @override
  // TODO: implement storeActions
  List<DDAction> get storeActions => [
        DDAction(
            name: "test1",
            callback: () {
              debugPrint("子视图2，收到test1事件");
            }),
        DDAction(
            name: "test2",
            callback: () async {
              debugPrint("子视图2收到test2事件,然后延迟3s");
              await Future.delayed(const Duration(seconds: 3));
            }),
      ];
}
